package com.health.es.service.impl;

import com.health.es.service.DictService;
import com.health.es.temp.DictionaryUtil;
import com.health.trans.common.entity.PageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.function.Supplier;

/**
 * @author nqlz
 */
@Service
@Slf4j
public class DictServiceImpl implements DictService {


    @Override
    public String getDictNameByKey(String appCode, String code, String version, String key, String realValue) {
        if (DictionaryUtil.DICT.containsKey(key) && DictionaryUtil.DICT.get(key).size() > 0) {
            return DictionaryUtil.DICT.get(key).getOrDefault(realValue, realValue);
        }
        synchronized (key.intern()) {
            if (DictionaryUtil.DICT.containsKey(key)) {
                return DictionaryUtil.DICT.get(key).getOrDefault(realValue, realValue);
            }
            //ApiResponse apiResponse = commonService.getDicChildren(appCode, code, version);
            //log.info(JSONObject.toJSONString(apiResponse));
            //
            ////List<DictResponse> dictResponses = Convert.toList(DictResponse.class, apiResponse.getData());
            ////
            ////Map<String, String> dictMap = dictResponses.stream()
            ////        .collect(Collectors.toMap(DictResponse::getCode, DictResponse::getName));
            //
            //DICT.put(key, dictMap);
            //
            //return dictMap.getOrDefault(realValue, realValue);
            return null;
        }
    }

    @Override
    public String getHospName(String hospCode) {

        if (DictionaryUtil.HOSP.containsKey(hospCode)) {
            return DictionaryUtil.HOSP.get(hospCode, () -> hospCode);
        }

        PageResponse pageResponse = null;//commonService.getHospName(hospCode, null, "2");
        Supplier<String> supplier = () -> {
            log.error(" 医疗机构代码 " + "code： " + hospCode + " 错误");
            return hospCode;
        };

        String hospName = Optional.ofNullable(pageResponse)
                .map(i -> {
                    synchronized (hospCode.intern()) {
                        if (DictionaryUtil.HOSP.containsKey(hospCode)) {
                            return DictionaryUtil.HOSP.get(hospCode, () -> hospCode);
                        }
                        //List<OrgResponse> orgResponses = Convert.toList(OrgResponse.class, i.getData());
                        //Map<String, String> collect = orgResponses.stream()
                        //        .peek(org -> HOSP.put(org.getPlatformOrgCode(), org.getPlatformOrgName()))
                        //        .collect(Collectors.toMap(OrgResponse::getPlatformOrgCode, OrgResponse::getPlatformOrgName));
                        //return collect.get(hospCode);
                        return null;
                    }
                }).orElseGet(supplier);

        return hospName;
    }
}
